﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CalemShop
{
    public partial class FrmCreateInvoiceSp : Form
    {
        private bool isOk = false;
        public FrmCreateInvoiceSp()
        {
            InitializeComponent();
            txtCost.KeyUp += new KeyEventHandler(c_KeyUp);
            txtCost.KeyPress += new KeyPressEventHandler(c_KeyPress);
        }

        private void FrmCreateInvoiceSp_Load(object sender, EventArgs e)
        {
            AutoCompleteStringCollection sr = new AutoCompleteStringCollection();
            sr.Add("Giảm giá sản phẩm 10%");
            sr.Add("Giảm giá sản phẩm 15%");
            sr.Add("Giảm giá sản phẩm 20%");
            sr.Add("Giảm giá đặc biệt cho người thân");
            sr.Add("Giảm giá đặc biệt cho bạn bè");
            sr.Add("Giảm giá đặc biệt cho khách quen");
            sr.Add("Mua sắm văn phòng phẩm");
            sr.Add("Bán hàng sale-off 10%");
            sr.Add("Bán hàng sale-off 15%");
            sr.Add("Bán hàng sale-off 20%");
            sr.Add("Bán hàng sale-off 25%");

            txtDesc.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtDesc.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtDesc.AutoCompleteCustomSource = sr;

            cbSeller.DataSource = Database.getUserList();
        }

        void c_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }



        void c_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if ((e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9) || (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9))
                {
                    TextBox control = (TextBox)sender;
                    string value = control.Text.Replace(@".", "");
                    int num;
                    Int32.TryParse(value, out num);
                    string s = num.ToString("#,0");
                    control.Tag = num;
                    control.Text = s;
                    control.SelectionStart = s.Length + 1;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCost.Text) || txtCost.Tag == null || string.IsNullOrEmpty(txtDesc.Text))
            {
                return;
            }

            try
            {
                decimal money = decimal.Parse(txtCost.Tag.ToString());
                if (money <= 0)
                {
                    return;
                }

                string customer = txtCustomer.Text;
                if (string.IsNullOrEmpty(customer))
                {
                    customer = "customer";
                }

                isOk = Database.createInvoiceOutcoming(cbSeller.Text, customer, txtDesc.Text, money) > 0;
            }
            catch (Exception ex)
            {
                isOk = true;
                MessageBox.Show(ex.Message);
                
            }

        }

        private void FrmCreateInvoiceSp_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!isOk)
            {
                e.Cancel = true;
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            isOk = true;
        }
    }
}
